package com.nce.web;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.util.StrutsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.nce.entity.MsgStats;
import com.nce.service.MsgService;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/admin")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "msgstats.action", type = "redirect") })
public class MsgstatsAction extends ActionSupport {
	
	@Autowired
	private MsgService msgService;

	private static final long serialVersionUID = -6999904901932084228L;
	private static Logger logger = LoggerFactory.getLogger(MsgstatsAction.class);
	private Page<MsgStats> page = new Page<MsgStats>(20);
	private String sdate ;
	private String edate;
	private String minwords;
	private String maxwords;
	private String group;
	private String vpstats;
	


	public String list(){			
		if(vpstats==null || vpstats.equals("0")){
			page = msgService.findQQMsgStatsPage(sdate,edate,minwords,maxwords,group,page);
		}else if(vpstats.equals("1")){
			page = msgService.findQQMsgStatsPageWithVp(sdate,edate,minwords,maxwords,group,page);
		}else if(vpstats.equals("2")){
			page = msgService.findQQMsgStatsPageWithoutVp(sdate,edate,minwords,maxwords,group,page);
		}
		return "msgstatsIndex";
	}
	
	
	public String export(){
		List<MsgStats> msgStats = null;
		
		if(vpstats==null || vpstats.equals("0")){
			msgStats = msgService.findQQMsgStatsList(sdate,edate,minwords,maxwords,group);
		}else if(vpstats.equals("1")){
			msgStats = msgService.findQQMsgStatsListWithVp(sdate,edate,minwords,maxwords,group);
		}else if(vpstats.equals("2")){
			msgStats = msgService.findQQMsgStatsListWithoutVp(sdate,edate,minwords,maxwords,group);
		}
		
		if(msgStats!=null && msgStats.size() > 0){
			String abc = Struts2Utils.getRequest().getRealPath("/down/");
			Format formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
		    String dateStr = formatter.format(new Date());
		    String downUrl = "http://127.0.0.1/down/"+dateStr+"_msgstat.csv";
		    	BufferedWriter out = null;
				try {
			        out = new BufferedWriter(new FileWriter(abc+"/"+dateStr+"_msgstat.csv"));
			        out.write(MsgStats.formatToCsvHeader());
			        int i = 1;
			        for(MsgStats msgStat : msgStats){
			        	out.write(msgStat.formatToCsv(i));
			        	i++;
			        	out.flush();
			        }		        
			        
			    } catch (IOException e) {
			    	logger.error("生成表格文件出错",e);
			    }finally{
			    	if(out!=null){
			    		try {
							out.close();
						} catch (IOException e) {
							logger.error("生成表格文件出错",e);
						}
			    	}
			    }
			    
			    try {
					Struts2Utils.getResponse().sendRedirect(downUrl);
				} catch (IOException e) {					
					logger.error("生成表格文件出错",e);
				}    
		}		
		
		return null;
	}
	
	public String index(){			
		return "msgstatsIndex";
	}
	
	
	
	
	
	
	public String getVpstats() {
		return vpstats;
	}




	public void setVpstats(String vpstats) {
		this.vpstats = vpstats;
	}




	public String getSdate() {
		return sdate;
	}




	public void setSdate(String sdate) {
		this.sdate = sdate;
	}




	public String getEdate() {
		return edate;
	}




	public void setEdate(String edate) {
		this.edate = edate;
	}




	public String getMinwords() {
		return minwords;
	}




	public void setMinwords(String minwords) {
		this.minwords = minwords;
	}




	public String getMaxwords() {
		return maxwords;
	}




	public void setMaxwords(String maxwords) {
		this.maxwords = maxwords;
	}




	public MsgService getMsgService() {
		return msgService;
	}
	public void setMsgService(MsgService msgService) {
		this.msgService = msgService;
	}
	
	public String getGroup() {
		return group;
	}
	public void setGroup(String group) {
		this.group = group;
	}
	public Page<MsgStats> getPage() {
		return page;
	}
	public void setPage(Page<MsgStats> page) {
		this.page = page;
	}



}